package SecondTry;

/**
 * Created with IntelliJ IDEA.
 * User: Gauss
 * Date: 07.12.13
 * Time: 21:44
 * To change this template use File | Settings | File Templates.
 */
public class Coordinates {
    protected int x;
    protected int y;

    public Coordinates(int x, int y) throws IncorrectCoordinatesException {
        if (x<0 || y<0) throw new IncorrectCoordinatesException();
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public void changeAccordingDirection(Direction direction, Coordinates measures) throws IncorrectCoordinatesException {
        if (direction==Direction.DOWN) {
            y--;
            if (isNotInMeasures(measures)) {
                y++;
                throw new IncorrectCoordinatesException();
            }
        }
        if (direction==Direction.UP) {
            y++;
            if (isNotInMeasures(measures)) {
                y--;
                throw new IncorrectCoordinatesException();
            }
        }
        if (direction==Direction.LEFT) {
            x--;
            if (isNotInMeasures(measures)) {
                x++;
                throw new IncorrectCoordinatesException();
            }
        }
        if (direction==Direction.RIGHT) {
            x++;
            if (isNotInMeasures(measures)) {
                x--;
                throw new IncorrectCoordinatesException();
            }
        }
    }

    /*"Operation" argument:
    * 0: increment operation
    * 1: decrement operation*//*
    private void modifyCoordinate(int coordinate, short operation) {
        if (operation==0) {
            coordinate++;
            if (isNotInMeasures())
        }
    }*/

    @Override
    public boolean equals(Object obj) {
        return obj instanceof Coordinates && ((Coordinates)obj).getX()==this.x && ((Coordinates)obj).getY()==this.y;
    }

    public boolean isNotInMeasures(Coordinates measures) {
        return (x>(measures.getX()-1) || y>(measures.getY()-1) || x<0 || y<0);
    }
}
